package smoothingExample;

import java.awt.image.BufferedImage;
import java.awt.image.Kernel;

import com.pearsoneduc.ip.io.ImageDecoder;
import com.pearsoneduc.ip.io.ImageEncoder;
import com.pearsoneduc.ip.io.ImageFile;
import com.pearsoneduc.ip.op.ConvolutionOp;

public class Sharpening {

	public static void main(String[] args) {
		 try {
             // see page 175
       ImageDecoder input = ImageFile.createImageDecoder("./image/esempio_interno.pgm");
       ImageEncoder output = ImageFile.createImageEncoder("./image/esempio_internoSharpening.pgm");

       int borderStrategy =1;

       int rescaleStrategy =1;
 

       // Load image and kernel

       BufferedImage inputImage = input.decodeAsBufferedImage();
     
      // P169
       int width = 3;
		int height = 3;
		float[] coeff  = new float[width*height];
		float[] coeff2 = new float[width*height];
		float[] result = new float[width*height];
		
		for (int i = 0; i < coeff.length; i++) {
			coeff[i]=1.0f/coeff.length;
			if(i==4){
			coeff2[i]=2;
			}
			
			result[i]= coeff2[i] - coeff[i];
		}
		Kernel kernel = new Kernel(width, height, result);
		
		
		// Create convolution operator and convolve image
		
       ConvolutionOp convOp = new ConvolutionOp(kernel,
        borderStrategy, ConvolutionOp.SINGLE_PASS, rescaleStrategy);

       BufferedImage outputImage = convOp.filter(inputImage, null);


       // Write results to output file

       output.encode(outputImage);
       System.exit(0);

     }
     catch (Exception e) {
       System.err.println(e);
       System.exit(1);
     }
   }
	}

